package com.segmentfault.mvc.dao.impl;

import com.segmentfault.mvc.dao.IGoodsDao;
import com.segmentfault.mvc.entity.GoodsEntity;
import com.segmentfault.mvc.util.MockData;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class GoodsDAOImpl implements IGoodsDao {
    @Override
    public List<GoodsEntity> selectAll() {
        return MockData.goodsEntities;
    }

    @Override
    public GoodsEntity selectById(Long goodsId) {
        int size = MockData.goodsEntities.size();
        for(int i = 0; i < size; i++) {
            GoodsEntity goodsEntity = MockData.goodsEntities.get(i);
            if(goodsEntity.getId() == goodsId) {
                return goodsEntity;
            }
        }
        return null;
    }

    @Override
    public void insert(GoodsEntity goodsEntity) {
        goodsEntity.setId(MockData.goodsEntities.size() + 1L);
        MockData.goodsEntities.add(goodsEntity);
    }

    @Override
    public void update(GoodsEntity goodsEntity) {
        int size = MockData.goodsEntities.size();
        for(int i = 0; i < size; i++) {
            GoodsEntity goods = MockData.goodsEntities.get(i);
            if(goodsEntity.getId() == goods.getId()) {
                MockData.goodsEntities.set(i, goodsEntity);
                break;
            }
        }
    }

    @Override
    public void delete(Long goodsId) {
        int size = MockData.goodsEntities.size();
        for(int i = 0; i < size; i++) {
            GoodsEntity goods = MockData.goodsEntities.get(i);
            if(goodsId == goods.getId()) {
                MockData.goodsEntities.remove(i);
                break;
            }
        }
    }
}
